package com.yxhl.zoume.core.car.presenter;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.car.model.SelectCityEntity;
import com.yxhl.zoume.core.car.view.common.SelectCityView;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.alphabet.AlphabetHelper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

/**
 * 项目名称：Ferrari
 * Created by menggod on 16/9/2 .
 */
public class SelectCityPresenter extends BasePresenter {
    private static final String TAG = "SelectCityPresenter";

    private String mDatabaseFilename;
    private List<SelectCityEntity> mCitylist = new ArrayList<>();
    private HashMap<String, List<SelectCityEntity>> alphabetMap = new HashMap<>();
    private SelectCityView mSelectCityView;

    @Inject
    public SelectCityPresenter(Context context) {
        super(context);
    }

    public void readDatabases() {
        SQLiteDatabase database = SQLiteDatabase.openDatabase(mDatabaseFilename, null, SQLiteDatabase.OPEN_READONLY);
        String sql = "select * from city ";
        Cursor cursor = database.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String center = cursor.getString(cursor.getColumnIndex("center"));
            SelectCityEntity selectCityEntity = new SelectCityEntity(name);
            selectCityEntity.setName(name);
            selectCityEntity.setCenter(center);
            AlphabetHelper.processCityList(alphabetMap, selectCityEntity);
            //       mCitylist.add(selectCityEntity);
        }
        cursor.close();
        database.close();
        LOG.d(TAG, "数据库查询完成");
        mCitylist = AlphabetHelper.getCityAlphabetData(alphabetMap);
        mSelectCityView.renderSelectCity(mCitylist);

    }


    /**
     * 拷贝数据库文件
     */
    public void copyFile() {
        new Thread(new Runnable() {


            @Override
            public void run() {
                try {
                    String filePath = mContext.getCacheDir().getAbsolutePath();
                    String DATABASE_PATH = filePath.substring(0, filePath.length() - 5) + "databases";
                    String DATABASE_FILENAME = "amap.db";
                    mDatabaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
                    File dir = new File(DATABASE_PATH);
                    if (!dir.exists()) {
                        dir.mkdir();
                    }
                    if (!(new File(mDatabaseFilename)).exists()) {
                        InputStream is = mContext.getResources().openRawResource(R.raw.amap);
                        FileOutputStream fos = new FileOutputStream(mDatabaseFilename);
                        byte[] buffer = new byte[8192];
                        int count = 0;
                        // 开始复制文件
                        while ((count = is.read(buffer)) > 0) {
                            fos.write(buffer, 0, count);
                        }
                        fos.close();
                        is.close();
                        LOG.d(TAG, "数据库拷贝完成");
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void attachView(BaseView view) {
        mSelectCityView = (SelectCityView) view;
    }
}
